using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Technoera.Common.Nh.CustomPropertyAccessors
{
    public class UnknowEntityProvider
    {
        private static UnknowEntityFactory _unknowEntityFactory;
        public static UnknowEntityFactory UnknowEntityFactory
        {
            get
            {
                if (_unknowEntityFactory == null)
                {
                    string concreteFactoryTypeName = System.Configuration.ConfigurationManager.AppSettings.Get("UnknowEntityFactoryType");

                    if (concreteFactoryTypeName == null)
                    {
                        throw new ApplicationException("Missing key \"UnknowEntityFactoryType\" in appConfig section.");
                    }

                    try
                    {
                        Type concreteType = Type.GetType(concreteFactoryTypeName);
                        Assembly assembly = concreteType.Assembly;
                        UnknowEntityFactory factory = (UnknowEntityFactory)assembly.CreateInstance(concreteType.FullName);
                        _unknowEntityFactory = (UnknowEntityFactory)factory;
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException("There was an error while initializing the concrete UnknowEntityFactory", ex);
                    }
                }

                return _unknowEntityFactory;
            }
            set
            {
                _unknowEntityFactory = value;
            }
        }
    }
}
